import tkinter as tk
from tkinter import ttk,messagebox
import pandas as pd
import os
import sqlite3
from tools import DataHelper
from uis.UI_TableApp import TableApp

class LoginWindow(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        
        self.root = parent
        #隐藏主窗口
        parent.withdraw()

        self.title("登录")
        self.geometry("300x250")

        # 用户名标签和输入框
        self.username_label = tk.Label(self, text="用户名:")
        self.username_label.pack(pady=5)
        self.username_entry = tk.Entry(self)
        self.username_entry.pack(pady=5)

        # 密码标签和输入框
        self.password_label = tk.Label(self, text="密码:")
        self.password_label.pack(pady=5)
        self.password_entry = tk.Entry(self, show="*")
        self.password_entry.pack(pady=5)

        # 登录按钮
        self.login_button = tk.Button(self, text="登录", command=self.login)
        self.login_button.pack(pady=10)

        self.username_entry.insert(0, 'admin')
        self.password_entry.insert(0, '123')

    def login(self):
        username = self.username_entry.get()
        password = self.password_entry.get()
        if self.validate_login(username, password):
            #messagebox.showinfo("成功", "登录成功")
            self.destroy()  # 关闭登录窗口
            self.root.deiconify()  # 显示主窗口
            TableApp(self.root)  # 创建并显示主应用程序界面
            #self.destroy()  # 关闭登录窗口
        else:
            messagebox.showerror("错误", "用户名或密码错误")

    def validate_login(self, username, password):
        # 这里可以添加实际的验证逻辑，例如查询数据库
        # 示例中仅使用硬编码的用户名和密码
        return username == "admin" and password == "123" 